package com.example.demo.syn;

public class ByTicket {
    public static void main(String[] args) {
        Buy buy = new Buy();
        new Thread(buy,"a").start();
        new Thread(buy,"b").start();
        new Thread(buy,"c").start();
    }
}
class Buy implements Runnable{
    private int ticket=10;
    boolean flag=true;
    @Override
    public  void run() {
        while (flag){
            try {
                buy();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
    private synchronized void buy() throws InterruptedException {
        if (ticket<=0){
            flag=false;
            return;
        }
        Thread.sleep(1000);
        System.out.println(Thread.currentThread().getName()+"拿到票"+ticket--);

    }
}